/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.common.util.GeneListComparator;
import ca.ubc.icapture.genapha.forms.KeggPathwayForm;
import ca.ubc.icapture.genapha.forms.PathwayForm;
import icapture.SQLMgr;
import icapture.beans.DB.Gene;
import icapture.beans.KeggGeneBean;
import icapture.beans.KeggPathwayBean;
import java.util.ArrayList;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.util.NodeList;

/**
 *
 * @author BTripp
 */
public class KeggPathwayAction extends Action
{

    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {
        KeggPathwayForm pathwayForm = (KeggPathwayForm) form;
        //perform what you need to

        KeggPathwayBean kpb = SQLMgr.getKeggPathwayByID(pathwayForm.getPathwayID());
        pathwayForm.setKeggPathwayBean(kpb);

        String baseURL = "http://www.genome.jp";

        StringBuilder sb = new StringBuilder();
        sb.append(baseURL);
        sb.append("/dbget-bin/show_pathway?");
        sb.append("hsa" + kpb.getId());

        ArrayList<Gene> geneList = new ArrayList<Gene>();
        for (KeggGeneBean kgb : kpb.getGeneList())
        {
            geneList.add(SQLMgr.getGene(kgb.getGeneId()));
            sb.append("+");
            sb.append(kgb.getKeggGeneId());
        }

        GeneListComparator c = new GeneListComparator("");
        Collections.sort(geneList, c);
        pathwayForm.setGeneList(geneList);

        PathwayForm pForm = new PathwayForm();
        pForm.setGeneList(geneList);
        pForm.setPathwayName(kpb.getName());
        request.getSession().setAttribute("pathwayForm", pForm);

        try
        {
            Lexer.STRICT_REMARKS = false;
            NodeList nodeList;
            Parser parser = new Parser(sb.toString());
            nodeList = parser.parse(new HasAttributeFilter("usemap"));

            if (nodeList.size() == 1)
            {
                String[] s = nodeList.elementAt(0).getText().split(" ");
                for (String p : s)
                {
                    if (p.startsWith("src="))
                    {
                        String url = p.substring(5, p.length() - 1);
                        pathwayForm.setURL(baseURL + url);
                    }
                }
            }

            nodeList = parser.parse(new TagNameFilter("area"));

            for (int i = 0; i < nodeList.size(); i++)
            {
                Node node = nodeList.elementAt(i);
                System.out.println(node.getText());
            }

        }
        catch (Exception e)
        {
            pathwayForm.setURL(sb.toString());
            e.printStackTrace();
        }

        return mapping.findForward("display");
    }
}
